﻿//服务端控件ID获取
var ClientIds1 = getClientIds1();

function openForm(action, id, baseInfoId) {
    if (baseInfoId != '') {
        var result = window.open("/Modules/ArchivesManagement/ArchInfoForm.aspx?id=" + id + "&action=" + action + "&baseInfoId=" + baseInfoId);
        if (result == "ok") {
            window.location.reload();
        }
    } else {
        alert('请先保存表单。');
    }
}

function deleteConfirm() {
    if (confirm('确定要删除该条记录？')) {
        return true;
    }
    return false;
}

function listSearch() {
    $(ClientIds1.btnSearch)[0].click();
}

function uploadFile(archInfoId) {
    var result = window.showModalDialog("/Modules/ArchivesManagement/FileUploader.aspx?archInfoId=" + archInfoId, self, "dialogheight: 450px; dialogwidth: 620px; center:yes; resizable:yes; scroll:no; status:no;");
    if (result == "ok") {
        //listSearch();
    }
}

function CheckChange(obj) {
    if ($(obj).parent().attr('class') == 'checkboxGroup1') {
        $('.checkboxGroup2').find('input').attr('checked', false);
        if ($('.checkboxGroup1').find('input').attr('checked')) {
            $("._selectItem").each(function () {
                $(this).attr("checked", true);
            });
        } else {
            $("._selectItem").each(function () {
                $(this).attr("checked", false);
            });
        }
    } else
        if ($(obj).parent().attr('class') == 'checkboxGroup2') {
            $('.checkboxGroup1').find('input').attr('checked', false);
            if ($('.checkboxGroup2').find('input').attr('checked')) {
                $("._selectItem").each(function () {
                    $(this).attr("checked", true);
                });
            } else {
                $("._selectItem").each(function () {
                    $(this).attr("checked", false);
                });
            }
        }
}

//销毁资料
function destory() {
    var selectItemsInfo = GetSelectedItem(); 
    if (selectItemsInfo == "") {
        alert('未选择任何记录，请先选择。');
        return;
    }
    if(confirm('确认要将所选资料信息销毁？')) {
        jQuery.ajax({
            url: "/Common/Services/BaseInfoFormHandler.aspx",
            type: "POST",
            dataType: "text",
            data: {
                method: 'DestoryArchInfos',
                ids: selectItemsInfo
            },
            success: function (result) {
                if (result == 'ok') {
                    alert('操作成功！');
                    listSearch();
                } else {
                    alert('操作失败！请联系管理员。错误原因：' + result);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert('系统错误 status:' + xmlHttpRequest.status + ' readyState:' + xmlHttpRequest.readyState + ' textStatus:' + textStatus);
            }
        });
    }
}

//获得所选信息
//返回格式：id1,id2,id3
function GetSelectedItem() {
    var selectedItems = "";
    $("input[type='checkbox']").each(function () {
        if (this.id == 'chkHeader' || this.id == 'MainHolder_CurrentPage') {
            return true;
        }
        if ($(this).attr("checked") && $(this).attr('class') == '_selectItem') {
            selectedItems += $(this).val() + ",";
        }
    });
    if (selectedItems != "")
        selectedItems = selectedItems.substr(0, selectedItems.length - 1);
    return selectedItems;
}

function uploadTemplateOld(baseInfoId) {
    if (baseInfoId == '') {
        alert('请先保存表单。');
        return;
    }

    var fileInfo = OpenFileUploaderWithFile("批量导入模板", "");
    //    var fileInfo = 'file;file';

    if (fileInfo) {
        jQuery.ajax({
            url: "/Common/Services/BaseInfoFormHandler.aspx",
            type: "POST",
            dataType: "text",
            data: {
                method: 'UploadArchInfos',
                file: fileInfo,
                baseInfoId: baseInfoId,
                templateType: 'old'
            },
            success: function (result) {
                if (result == 'ok') {
                    alert('导入成功！');
                    listSearch();
                } else {
                    alert('导入失败！原因：' + result);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert('系统错误 status:' + xmlHttpRequest.status + ' readyState:' + xmlHttpRequest.readyState + ' textStatus:' + textStatus);
            }
        });
    }
}

function uploadTemplate(baseInfoId) {
    if (baseInfoId == '') {
        alert('请先保存表单。');
        return;
    }
    var template = "/Resources/Templates/资料信息导入模板-单个.xls";

    var fileInfo = OpenFileUploaderWithFile("批量导入模板", template);
//    var fileInfo = 'file;file';

    if (fileInfo) {
        jQuery.ajax({
            url: "/Common/Services/BaseInfoFormHandler.aspx",
            type: "POST",
            dataType: "text",
            data: {
                method: 'UploadArchInfos',
                file: fileInfo,
                baseInfoId: baseInfoId
            },
            success: function (result) {
                if (result == 'ok') {
                    alert('导入成功！');
                    listSearch();
                } else {
                    alert('导入失败！原因：' + result);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert('系统错误 status:' + xmlHttpRequest.status + ' readyState:' + xmlHttpRequest.readyState + ' textStatus:' + textStatus);
            }
        });
    }
}

function exportData() {
    $(ClientIds1.exportBtn)[0].click();
}